Option Explicit

Private Const TEST_TABLE_NAME As String = "table1"
Private Const TEST_POINT As String = "."
Private Const TEST_COL_NAME As String = "col1"
Private Const TEST_SHARE_LINE_COMMENT As String = "--aaaaaa"
Private Const TEST_SINGLE_LINE_COMMENT As String = "--bbbbb"

' ======================================================================
' 执行所有测试用例
' ======================================================================
Public Sub test_all_case()
    ' ==================================================
    ' Public 方法
    ' ==================================================
    ' 添加条件
    test_ThisClass_addCondition

    ' ==================================================
    ' ISqlFormatterElem 接口实现测试
    ' ==================================================
    ' 测试: addComponent 方法
    test_ISqlFormatterElem_addComponent
    ' 测试: addComponentToFirst 方法
    test_ISqlFormatterElem_addComponentToFirst
    ' 测试: canAddComponentToFirst 方法
    test_ISqlFormatterElem_canAddComponentToFirst
    ' 测试: getAlignmentElemGroups 方法
    test_ISqlFormatterElem_getAlignmentElemGroups
    ' 测试: setElemNestCount 方法
    test_ISqlFormatterElem_setElemNestCount
    ' 测试: setPrefixSpaceCount 方法
    test_ISqlFormatterElem_setPrefixSpaceCount
    ' 测试: hasBoundaryParenthesis 方法
    test_ISqlFormatterElem_hasBoundaryParenthesis
    ' 测试: addBoundaryLeftParenthesis 方法
    test_ISqlFormatterElem_addBoundaryLeftParenthesis
    ' 测试: addBoundaryRightParenthesis 方法
    test_ISqlFormatterElem_addBoundaryRightParenthesis
    ' 测试: getType 方法
    test_ISqlFormatterElem_getType
    ' 测试: isComment 方法
    test_ISqlFormatterElem_isComment
    ' 测试: isShareLineComment 方法
    test_ISqlFormatterElem_isShareLineComment
    ' 测试: isSingleLineComment 方法
    test_ISqlFormatterElem_isSingleLineComment
    ' 测试: isEndsWithComment 方法
    test_ISqlFormatterElem_isEndsWithComment
    ' 测试: isMultiLine 方法
    test_ISqlFormatterElem_isMultiLine
    ' 测试: toString + toStringArray 方法
    test_ISqlFormatterElem_toString_toStringArray
End Sub


' ======================================================================
' 测试通用处理
' ======================================================================

' || 运算符
Private Function getStrConcatToken() As SqlToken
    Set getStrConcatToken = New SqlToken
    getStrConcatToken.Init "||", SqlTokenType.OPERA_STR_CONCAT
End Function

' + 运算符
Private Function getPlusToken() As SqlToken
    Set getPlusToken = New SqlToken
    getPlusToken.Init "+", SqlTokenType.OPERA_PLUS
End Function

' case 的 when 运算符
Private Function getWhenToken() As SqlToken
    Set getWhenToken = New SqlToken
    getWhenToken.Init "when", SqlTokenType.KEY_WHEN
End Function

' case 的 else 运算符
Private Function getElseToken() As SqlToken
    Set getElseToken = New SqlToken
    getElseToken.Init "else", SqlTokenType.KEY_ELSE
End Function

' case 的 Then 运算符
Private Function getThenToken() As SqlToken
    Set getThenToken = New SqlToken
    getThenToken.Init "then", SqlTokenType.KEY_THEN
End Function

' 获取逻辑运算符: and
Private Function getAndToken() As SqlToken
    Set getAndToken = New SqlToken
    getAndToken.Init "and", SqlTokenType.KEY_AND
End Function

' 获取逻辑运算符: or
Private Function getOrToken() As SqlToken
    Set getOrToken = New SqlToken
    getOrToken.Init "or", SqlTokenType.KEY_OR
End Function

' 获取逻辑运算符: Not
Private Function getNotToken() As SqlToken
    Set getNotToken = New SqlToken
    getNotToken.Init "not", SqlTokenType.KEY_NOT
End Function

' 获取一个条件运算符: <=
Private Function getLeConditionOperator() As SqlToken
    Set getLeConditionOperator = New SqlToken
    getLeConditionOperator.Init "<=", SqlTokenType.OPERA_LE
End Function

' 获取一个条件运算符: =
Private Function getEqConditionOperator() As SqlToken
    Set getEqConditionOperator = New SqlToken
    getEqConditionOperator.Init "=", SqlTokenType.OPERA_EQ
End Function

' 获取一个任意名称的 STR_NAME token
Private Function getStrNameToken(ByRef s As String) As SqlToken
    Set getStrNameToken = New SqlToken
    getStrNameToken.Init s, SqlTokenType.STR_NAME
End Function

' 获取表名 token
Private Function getTableNameToken() As SqlToken
    Set getTableNameToken = New SqlToken
    getTableNameToken.Init TEST_TABLE_NAME, SqlTokenType.STR_NAME
End Function

' 获取 [.] token
Private Function getPointToken() As SqlToken
    Set getPointToken = New SqlToken
    getPointToken.Init TEST_POINT, SqlTokenType.POINT
End Function

' 获取列名 token
Private Function getColNameToken() As SqlToken
    Set getColNameToken = New SqlToken
    getColNameToken.Init TEST_COL_NAME, SqlTokenType.STR_NAME
End Function

' 获取和前一个元素在同一行的注释
Private Function getShareLineCommentToken() As SqlToken
    Set getShareLineCommentToken = New SqlToken
    getShareLineCommentToken.Init TEST_SHARE_LINE_COMMENT, SqlTokenType.SHARE_LINE_COMMENT
End Function

' 获取一个单行的注释
Private Function getSingleLineCommentToken() As SqlToken
    Set getSingleLineCommentToken = New SqlToken
    getSingleLineCommentToken.Init TEST_SINGLE_LINE_COMMENT, SqlTokenType.SINGLE_LINE_COMMENT
End Function

' 获取一个逗号的元素
Private Function getCommaToken() As SqlToken
    Set getCommaToken = New SqlToken
    getCommaToken.Init ",", SqlTokenType.COMMA
End Function

' 获取一个没有边界括号的多行元素
' 多行连接的结果:
' col1 & ","
' col2
Private Function getMultiLineElemNoParenthesis() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")

    ' 2. 手动设置为多行元素，仍然是单行
    g.setNeedMultiLineWhenHasComma True

    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    Set getMultiLineElemNoParenthesis = elem
End Function

' 获取一个没有边界括号的多行元素
' 多行连接的结果:
' col1 & ","
' col2 --aaaaaa
Private Function getMultiLineElemNoParenthesisEndWithComment() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")

    ' 2. 手动设置为多行元素，仍然是单行
    g.setNeedMultiLineWhenHasComma True

    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    ' 5. 增加一个和前一个元素同一行的注释
    elem.addComponent getShareLineCommentToken

    Set getMultiLineElemNoParenthesisEndWithComment = elem
End Function

' 获取一个无逻辑运算符的条件
' 连接的结果:
' a = b
Private Function getNoLogicOpCondition()
    Dim result As SqlFormatterTwoElemCondition
    Set result = New SqlFormatterTwoElemCondition

    ' 设置第一个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("a")
    ' 设置条件运算符 =
    result.ISqlFormatterCondition_setConditionOperator getEqConditionOperator
    ' 设置第二个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("b")

    Set getNoLogicOpCondition = result
End Function

' 获取一个And逻辑运算符开头的条件
' 连接的结果:
' and a <= b
Private Function getAndCondition()
    Dim result As SqlFormatterTwoElemCondition
    Set result = New SqlFormatterTwoElemCondition

    ' 设置一个逻辑运算符
    result.ISqlFormatterCondition_setLogicOperator getAndToken
    ' 设置第一个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("a")
    ' 设置条件运算符 =
    result.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    ' 设置第二个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("b")

    Set getAndCondition = result
End Function

' 获取一个And逻辑运算符开头的多行条件
' 连接的结果:
' and a <= col1,
'          col2
Private Function getAndMultiLIneCondition()
    Dim result As SqlFormatterTwoElemCondition
    Set result = New SqlFormatterTwoElemCondition

    ' 设置一个逻辑运算符
    result.ISqlFormatterCondition_setLogicOperator getAndToken
    ' 设置第一个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("a")
    ' 设置条件运算符 =
    result.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    ' 设置第二个元素
    result.ISqlFormatterElem_addComponent getMultiLineElemNoParenthesis

    Set getAndMultiLIneCondition = result
End Function

' 获取一个Or逻辑运算符开头的条件
' 连接的结果:
' or a <= b
Private Function getOrCondition()
    Dim result As SqlFormatterTwoElemCondition
    Set result = New SqlFormatterTwoElemCondition

    ' 设置一个逻辑运算符
    result.ISqlFormatterCondition_setLogicOperator getOrToken
    ' 设置第一个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("a")
    ' 设置条件运算符 =
    result.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    ' 设置第二个元素
    result.ISqlFormatterElem_addComponent getStrNameToken("b")

    Set getOrCondition = result
End Function

' ======================================================================
' Public 方法
' ======================================================================
' 添加条件
Public Sub test_ThisClass_addCondition()
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration
    Dim strArr As IPrimitiveArrayRef

    Dim g As SqlFormatterMultiConditions

    Dim prefixSpace As String
    prefixSpace = "    "
    ' ==============================
    ' 1. 添加一个无逻辑运算符的单行条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    g.addCondition getNoLogicOpCondition

    ' 检查: 是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "a = b"

    ' ==============================
    ' 2. 添加一个无逻辑运算符的单行条件 + 无逻辑运算符的
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    ' 添加一个无逻辑运算符的单行条件
    g.addCondition getNoLogicOpCondition
    ' 添加一个And逻辑运算符的单行条件
    g.addCondition getAndCondition

    ' 检查: 是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 设置对齐长度
    g.ISqlFormatterElem_setPrefixSpaceCount 4, config
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "    a = b"
    Debug.Assert strArr.Item(1) = "and a <= b"

    ' ==============================
    ' 3. 嵌套条件
    ' ==============================
    ' and not (
    '            a = b
    '         or a <= b
    '     )

    ' 1. 创建内层的的条件
    Set g = New SqlFormatterMultiConditions
    ' 添加一个无逻辑运算符的单行条件
    g.addCondition getNoLogicOpCondition
    ' 添加一个And逻辑运算符的单行条件
    g.addCondition getOrCondition
    ' 增加边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis
    g.ISqlFormatterElem_addBoundaryRightParenthesis

    ' 2. 创建外层的条件对象
    Dim outCondition As SqlFormatterTwoElemCondition
    Set outCondition = New SqlFormatterTwoElemCondition

    ' 设置一个逻辑运算符
    outCondition.ISqlFormatterCondition_setLogicOperator getAndToken
    ' 设置 Not
    outCondition.ISqlFormatterElem_addComponent getNotToken
    ' 设置内侧条件
    outCondition.ISqlFormatterElem_addComponent g

    outCondition.ISqlFormatterElem_setPrefixSpaceCount 0, config
    Set strArr = outCondition.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 4
    Debug.Assert strArr.Item(0) = "and not ("
    Debug.Assert strArr.Item(1) = "            a = b"
    Debug.Assert strArr.Item(2) = "        or  a <= b"
    Debug.Assert strArr.Item(3) = "    )"

    ' 检查: 是多行的
    Debug.Assert outCondition.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert outCondition.ISqlFormatterElem_isEndsWithComment = False

End Sub

' ======================================================================
' ISqlFormatterElem 接口实现测试
' ======================================================================
' 测试: addComponent 方法
Private Sub test_ISqlFormatterElem_addComponent()
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim g As SqlFormatterMultiConditions

    ' ==============================
    ' 1. 添加多个单行条件
    ' ==============================
    ' 1. 添加第一个单行元素
    Set g = New SqlFormatterMultiConditions

    g.addCondition getNoLogicOpCondition

    Debug.Assert g.ISqlFormatterElem_toString(config) = "a = b"

    ' 检查: 是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 2. 添加一个单行元素
    g.ISqlFormatterElem_addComponent getStrNameToken("x1")

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "a = b"
    Debug.Assert strArr.Item(1) = "x1"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 3. 添加第二个单行条件
    g.addCondition getAndCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "a = b"
    Debug.Assert strArr.Item(1) = "x1"
    Debug.Assert strArr.Item(2) = "and a <= b"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' ==============================
    ' 2. 添加一个单行条件和同行注释
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 添加一个单行条件
    g.addCondition getNoLogicOpCondition

    ' 3. 添加一个同行注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken

    ' 检查是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "a = b --aaaaaa"

    ' 4. 增加左边界括号，变成多行的
    g.ISqlFormatterElem_addBoundaryLeftParenthesis
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "("
    Debug.Assert strArr.Item(1) = "    a = b --aaaaaa"

    ' 5. 增加右边界括号，变成多行的
    g.ISqlFormatterElem_addBoundaryRightParenthesis
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "("
    Debug.Assert strArr.Item(1) = "    a = b --aaaaaa"
    Debug.Assert strArr.Item(2) = ")"

    ' ==============================
    ' 3. 添加一个多行元素，然后再添加一个单行元素
    ' ==============================
    ' 1. 添加第一个多行元素
    Set g = New SqlFormatterMultiConditions

    g.addCondition getAndMultiLIneCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "and a <= col1,"
    Debug.Assert strArr.Item(1) = "         col2"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 2. 添加一个单行元素
    g.ISqlFormatterElem_addComponent getStrNameToken("x1")
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "and a <= col1,"
    Debug.Assert strArr.Item(1) = "         col2"
    Debug.Assert strArr.Item(2) = "x1"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 3. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "and a <= col1,"
    Debug.Assert strArr.Item(1) = "         col2"
    Debug.Assert strArr.Item(2) = "x1 --aaaaaa"
End Sub

' 测试: addComponentToFirst 方法
Private Sub test_ISqlFormatterElem_addComponentToFirst()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim prefixSpace As String
    prefixSpace = "    "

    ' ==============================
    ' 2. 测试: 增加一个单行条件，然后在前面增加一个单行元素
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 增加一个单行条件
    g.addCondition getNoLogicOpCondition
    ' 2. 在前面增加一个同行注释元素
    g.ISqlFormatterElem_addComponentToFirst getShareLineCommentToken
    ' 2. 在前面增加一个单行元素
    g.ISqlFormatterElem_addComponentToFirst getStrNameToken("x1")

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "x1 --aaaaaa"
    Debug.Assert strArr.Item(1) = "a = b"

    ' 2. 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 3. 检查不是以注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' ==============================
    ' 3. 测试: 增加一个单行条件 + 一个和前一个元素同一行的注释
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    ' 1. 增加后一个注释
    g.ISqlFormatterElem_addComponentToFirst getShareLineCommentToken
    ' 2. 增加前一个单行条件
    g.addCondition getNoLogicOpCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "--aaaaaa"
    Debug.Assert strArr.Item(1) = "a = b"

    ' 3. 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 4. 检查不是以注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False


    ' ==============================
    ' 4. 测试: 增加一个和前一个元素同一行的注释 + 一个单行条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    ' 1. 增加后一个单行条件
    g.addCondition getNoLogicOpCondition
    ' 2. 增加前一个注释
    g.ISqlFormatterElem_addComponentToFirst getShareLineCommentToken

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = TEST_SHARE_LINE_COMMENT
    Debug.Assert strArr.Item(1) = "a = b"

    ' 3. 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 4. 检查不是以注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' ==============================
    ' 5. 测试: 增加单行条件 + 一个单行注释
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    ' 1. 增加前一个单行元素
    g.addCondition getNoLogicOpCondition
    ' 2. 增加后一个注释
    g.ISqlFormatterElem_addComponentToFirst getSingleLineCommentToken

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = TEST_SINGLE_LINE_COMMENT
    Debug.Assert strArr.Item(1) = "a = b"

    ' 3. 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 4. 检查不是以注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

End Sub

' 测试: canAddComponentToFirst 方法
Private Sub test_ISqlFormatterElem_canAddComponentToFirst()
    Dim g As SqlFormatterMultiConditions

    ' ==============================
    ' 1. 只增加一个单行元素: True
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    g.ISqlFormatterElem_addComponent getTableNameToken
    Debug.Assert g.ISqlFormatterElem_canAddComponentToFirst = True

    ' ==============================
    ' 2. 只增加左边界括号: False
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    g.ISqlFormatterElem_addBoundaryLeftParenthesis
    Debug.Assert g.ISqlFormatterElem_canAddComponentToFirst = False

    ' ==============================
    ' 3. 只增加右边界括号: True
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    g.ISqlFormatterElem_addBoundaryRightParenthesis
    Debug.Assert g.ISqlFormatterElem_canAddComponentToFirst = True

    ' ==============================
    ' 4. 添加左右边界括号: True
    ' ==============================
    Set g = New SqlFormatterMultiConditions
    g.ISqlFormatterElem_addBoundaryLeftParenthesis
    g.ISqlFormatterElem_addBoundaryRightParenthesis
    Debug.Assert g.ISqlFormatterElem_canAddComponentToFirst = False
End Sub

' 测试: getAlignmentElemGroups 方法
Private Sub test_ISqlFormatterElem_getAlignmentElemGroups()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim condition As SqlFormatterTwoElemCondition
    Dim condition2 As SqlFormatterTwoElemCondition

    Dim groups As IObjArrayRef
    Dim group As IObjArrayRef

    ' ==============================
    ' 2. 在添加元素之前进行检查
    ' ==============================
    Debug.Assert g.ISqlFormatterElem_getAlignmentElemGroups Is Nothing

    ' ==============================
    ' 3. 添加第一个单行元素后再检查
    ' ==============================
    g.addCondition getTableNameToken
    Debug.Assert g.ISqlFormatterElem_getAlignmentElemGroups Is Nothing

    ' ==============================
    ' 4. 添加第一个条件对象
    ' ==============================
    Set condition = New SqlFormatterTwoElemCondition
    g.addCondition condition

    Set groups = g.ISqlFormatterElem_getAlignmentElemGroups
    Debug.Assert groups.Count = 1
    Set group = groups.Item(0)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition

    ' ==============================
    ' 5. 添加第二个条件对象
    ' ==============================
    Set condition2 = New SqlFormatterTwoElemCondition
    g.addCondition condition2

    Set groups = g.ISqlFormatterElem_getAlignmentElemGroups
    Debug.Assert groups.Count = 1
    Set group = groups.Item(0)
    Debug.Assert group.Count = 2
    ' 检查第一个条件
    Debug.Assert group.Item(0) Is condition
    ' 检查第二个条件
    Debug.Assert group.Item(1) Is condition2

    ' ==============================
    ' 6. 添加一个包含多个条件对象的组合对象
    ' ==============================
    ' 创建一个包含多个条件的组合对象
    Dim multiConditions As SqlFormatterCommaDeliMultiElem
    Set multiConditions = New SqlFormatterCommaDeliMultiElem
    Dim condition3 As SqlFormatterTwoElemCondition
    Set condition3 = New SqlFormatterTwoElemCondition
    Dim condition4 As SqlFormatterTwoElemCondition
    Set condition4 = New SqlFormatterTwoElemCondition
    multiConditions.ISqlFormatterElem_addComponent condition3
    multiConditions.ISqlFormatterElem_addComponent condition4

    ' 将这个组合对象保存到 elem
    g.addCondition multiConditions

    Set groups = g.ISqlFormatterElem_getAlignmentElemGroups
    Debug.Assert groups.Count = 1
    Set group = groups.Item(0)
    Debug.Assert group.Count = 4
    ' 检查第一个条件
    Debug.Assert group.Item(0) Is condition
    ' 检查第二个条件
    Debug.Assert group.Item(1) Is condition2
    ' 检查第三个条件
    Debug.Assert group.Item(2) Is condition3
    ' 检查第四个条件
    Debug.Assert group.Item(3) Is condition4

End Sub

' 测试: setElemNestCount 方法
Private Sub test_ISqlFormatterElem_setElemNestCount()
    ' 这里使用一个条件对象作为函数体，并通过对齐长度来检查

    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim g As SqlFormatterMultiConditions

    Dim condition As SqlFormatterTwoElemCondition

    ' ==============================
    ' 1. 测试: 增加一个单行的条件元素
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 创建一个条件对象
    Set condition = New SqlFormatterTwoElemCondition
    '    设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")

    ' 2. 添加这个条件对象
    g.addCondition condition

    ' 3. 设置嵌套深度
    g.ISqlFormatterElem_setElemNestCount 2, config

    ' 4. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 17 ' 8 + 9

    ' ==============================
    ' 2. 测试: 增加一个单行条件 + 多行元素 + 单行条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 创建一个单行条件对象，并添加这个单行条件对象
    Set condition = New SqlFormatterTwoElemCondition
    '    设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")
    '    添加这个单行元素
    g.addCondition condition

    ' 2. 增加一个多行元素
    g.ISqlFormatterElem_addComponent getMultiLineElemNoParenthesis

    ' 3. 创建第二个单行条件对象，并添加这个单行条件对象
    Dim condition2 As SqlFormatterTwoElemCondition
    Set condition2 = New SqlFormatterTwoElemCondition
    '    设置 and
    condition2.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition2.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition2.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition2.ISqlFormatterElem_addComponent getStrNameToken("x2")
    '    添加这个单行元素
    g.addCondition condition2

    ' 4. 设置嵌套深度
    g.ISqlFormatterElem_setElemNestCount 2, config

    ' 5. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 17 ' 8 + 9
    Debug.Assert condition2.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 17 ' 8 + 9

End Sub

' 测试: setPrefixSpaceCount 方法
Private Sub test_ISqlFormatterElem_setPrefixSpaceCount()
    ' 这里使用一个条件对象作为函数体，并通过对齐长度来检查

    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim g As SqlFormatterMultiConditions

    Dim condition As SqlFormatterTwoElemCondition

    ' ==============================
    ' 1. 测试: 增加一个单行的条件元素
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 创建一个条件对象
    Set condition = New SqlFormatterTwoElemCondition
    '    设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")

    ' 2. 添加这个条件对象
    g.addCondition condition

    ' 3. 设置前缀空格数量
    g.ISqlFormatterElem_setPrefixSpaceCount 10, config

    ' 4. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 19 ' 10 + 9

    ' ==============================
    ' 2. 测试: 增加一个单行条件 + 多行元素 + 单行条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 创建一个单行条件对象，并添加这个单行条件对象
    Set condition = New SqlFormatterTwoElemCondition
    '    设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")
    '    添加这个单行元素
    g.addCondition condition

    ' 2. 增加一个多行元素
    g.ISqlFormatterElem_addComponent getMultiLineElemNoParenthesis

    ' 3. 创建第二个单行条件对象，并添加这个单行条件对象
    Dim condition2 As SqlFormatterTwoElemCondition
    Set condition2 = New SqlFormatterTwoElemCondition
    '    设置 and
    condition2.ISqlFormatterCondition_setLogicOperator getAndToken
    '    设置第一个元素
    condition2.ISqlFormatterElem_addComponent getStrNameToken("x1")
    '    设置条件运算符 <=
    condition2.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    '    设置第二个元素
    condition2.ISqlFormatterElem_addComponent getStrNameToken("x2")
    '    添加这个单行元素
    g.addCondition condition2

    ' 4. 设置前缀空格数量
    g.ISqlFormatterElem_setPrefixSpaceCount 10, config

    ' 5. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 19 ' 10 + 9
    Debug.Assert condition2.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 19 ' 10 + 9

End Sub

' 测试: hasBoundaryParenthesis 方法
Private Sub test_ISqlFormatterElem_hasBoundaryParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 测试
    ' ==============================
    ' 2. 增加左边界括号
    elem.addBoundaryLeftParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True

    ' 3. 增加右边界括号
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True

    ' 4. 重新创建对象，并只添加右边界括号
    Set g = New SqlFormatterMultiConditions
    Set elem = g

    elem.addComponent getTableNameToken
    Debug.Assert elem.hasBoundaryParenthesis = False

    '    增加右边界括号
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True
End Sub

' 测试: addBoundaryLeftParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryLeftParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis
    Debug.Assert elem.toString(config) = "(" & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "(" & TEST_TABLE_NAME

    ' ==============================
    ' 4. 添加多个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis(3)
    Debug.Assert elem.toString(config) = "((((" & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((((" & TEST_TABLE_NAME
End Sub

' 测试: addBoundaryRightParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryRightParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & ")"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & ")"

    ' ==============================
    ' 4. 添加多个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis(3)
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & "))))"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & "))))"
End Sub

' 测试: getType 方法
Private Sub test_ISqlFormatterElem_getType()
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    ' 默认为 NO_MEAN
    Debug.Assert g.ISqlFormatterElem_getType = SqlTokenType.NO_MEAN
End Sub

' 测试: isComment 方法
Private Sub test_ISqlFormatterElem_isComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g
    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isComment = False
End Sub

' 测试: isShareLineComment 方法
Private Sub test_ISqlFormatterElem_isShareLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isShareLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isShareLineComment = False
End Sub

' 测试: isSingleLineComment 方法
Private Sub test_ISqlFormatterElem_isSingleLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterMultiConditions
    Set g = New SqlFormatterMultiConditions

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isSingleLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isSingleLineComment = False
End Sub

' 测试: isEndsWithComment 方法
Private Sub test_ISqlFormatterElem_isEndsWithComment()
    ' 参考: test_ISqlFormatterElem_addComponent
End Sub

' 测试: isMultiLine 方法
Private Sub test_ISqlFormatterElem_isMultiLine()
    ' 参考: test_ISqlFormatterElem_addComponent
End Sub

' 测试: toString + toStringArray 方法
Private Sub test_ISqlFormatterElem_toString_toStringArray()
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim g As SqlFormatterMultiConditions

    Dim prefixSpace As String
    prefixSpace = "    "

    ' ==============================
    ' 1. 添加一个单行条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 添加一个单行条件
    g.addCondition getNoLogicOpCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "a = b"

    ' 2. 设置边界括号: 左边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((a = b"

    ' 3. 设置边界括号: 左 + 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((a = b))"

    ' 4. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = "    a = b --aaaaaa"
    Debug.Assert strArr.Item(2) = "))"

    ' 5. 添加一个单行元素，并只添加右括号
    Set g = New SqlFormatterMultiConditions

    g.addCondition getNoLogicOpCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "a = b"

    '  设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "a = b))"

    ' ==============================
    ' 2. 添加一个多行的条件
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 添加一个多行的条件
    g.addCondition getAndMultiLIneCondition

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "and a <= col1,"
    Debug.Assert strArr.Item(1) = "         col2"

    ' 2. 设置边界括号: 左边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = prefixSpace & "and a <= col1,"
    Debug.Assert strArr.Item(2) = prefixSpace & "         col2"


    ' 3. 设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 4
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = prefixSpace & "and a <= col1,"
    Debug.Assert strArr.Item(2) = prefixSpace & "         col2"
    Debug.Assert strArr.Item(3) = "))"

    ' 5. 添加一个多行的元素 + 单行元素，并只设置右边界括号
    Set g = New SqlFormatterMultiConditions
    g.addCondition getAndMultiLIneCondition

    '  设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = prefixSpace & "and a <= col1,"
    Debug.Assert strArr.Item(1) = prefixSpace & "         col2"
    Debug.Assert strArr.Item(2) = "))"

    ' ==============================
    ' 3. 特殊case: 左括号 + 一个同行注释 + 一个元素
    ' ==============================
    Set g = New SqlFormatterMultiConditions

    ' 1. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    ' 2. 添加多个单行元素
    g.ISqlFormatterElem_addComponent getStrNameToken("x1")
    ' 3. 设置边界括号: 左边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "( --aaaaaa"
    Debug.Assert strArr.Item(1) = "    x1"
End Sub